<?php
namespace api;
use controller;
use model\cartModel;
use model\goodsModel;

class cart extends controller{


    public function __construct($container)
    {
        parent::__construct($container);

        //todo 登录校验
        $res = [
            'status'=>'1',
            'description'=>'请先登录',
        ];
        if( empty($_SESSION['customer_id']) ){
            echo json_encode( $res );
            die();
        }
    }

    public function index(){

        $res = [
            'list'=>[]
        ];
        $cart_ids = null;
        if( isset( $_GET['cart_ids'] ) ){
            $cart_ids = [];
            if( !empty( $_GET['cart_ids'] ) ){
                $cart_ids = explode( ',', $_GET['cart_ids'] );
            }else{
                echo json_encode( $res );
            }
        }

        $customer_id = $_SESSION['customer_id'];
        $list = cartModel::get_list( $customer_id, $cart_ids );

//        $goods_total = 0;
//        $order_total = 0;
//        foreach( $list as &$item ){
//            $item['img_url'] = goodsModel::getImgUrl( $item['goods_id'] );
//            $goods_total += $item['price'] * $item['goods_num'];
//
//        }
//        $res['goods_total'] = $goods_total;
//        $res['order_total'] = $goods_total;
        $res['list'] = $list;

        echo json_encode( $res );


    }

    public function filter(){

    }

    public function add(){

        $res = [];
        //todo 获取 customer_id, 如果没有登录，返回（后期可以在登录前 添加购物车）
        if( !empty( $_SESSION['customer_id'] ) ){
            $customer_id = $_SESSION['customer_id'];
        }else{
            $res = [
                'status' => 1,
                'description'=> '请先登录'
            ];
            echo json_encode( $res );
            die();
        }


        //todo 获取 添加的产品, 以及数量
        $product_id = $_GET['product_id'];
        $number = $_GET['number'];
        $number = intval( $number );
        cartModel::add( $product_id, $number, $customer_id );

        $res = [
            'status' => 0,
            'description'=> '添加成功'
        ];

        //todo 添加到数据库

        echo json_encode( $res );

    }

    public function remove(){

        $cart_id = $_GET['cart_id'];
        $res = cartModel::remove( $cart_id );



        echo json_encode( $res );
    }

    public function change(  ){
        $cart_id = $_GET['cart_id'];
        $number = $_GET['number'];
        $res = [
            'status'=>0
        ];

        cartModel::change( $cart_id, $number );

        echo json_encode( $res );

    }

    /**
     * 根据所选计算价格
     */
    public function calculate(){

        $res = [
            'goods_total'=>0,
            'order_total'=>0,
        ];
        $cart_ids = null;
        $cart_ids = [];
        if( !empty( $_GET['cart_ids'] ) ){
            $cart_ids = explode( ',', $_GET['cart_ids'] );
        }else{
            echo json_encode( $res );
            die();
        }

        $customer_id = $_SESSION['customer_id'];
        $list = cartModel::get_list( $customer_id, $cart_ids );

        $goods_total = 0;
        $order_total = 0;
        foreach( $list as &$item ){
//            $item['img_url'] = goodsModel::getImgUrl( $item['goods_id'] );
            $goods_total += $item['price'] * $item['goods_num'];

        }
        $res['goods_total'] = $goods_total;
        $res['order_total'] = $goods_total;
//        $res['list'] = $list;

        echo json_encode( $res );

    }

}